<template>
  <el-container class="el-container">
    <!-- 头部 -->
    <el-header class="el-header">
      <div>
        <img src="../assets/img/psb.webp" alt="" />
        <span>电商管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 整体 -->
    <el-container>
      <!-- 侧边栏 -->
      <!-- 绑定属性与if判断的结合使用 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleColl">|||</div>
        <!-- 侧边栏菜单 --><!-- 选中的颜色 -->
        <el-menu
          :collapse="isCollapse"
          :collapse-transition="false"
          class="el-menu"
          unique-opened
          background-color="#333744"
          text-color="#fff"
          active-text-color="#ffd04b"
          router
          :default-active="activePath"
        >
          <!-- :collapse="isCollapse" 折叠 -->
          <!-- :collapse-transition="false" 动画效果取消 -->
          <!-- 一级菜单 -->
          <el-submenu
            class="el-submenu"
            v-for="item in menulist"
            :key="item.id"
            :index="item.id + ''"
          >
            <!-- 模板区域 -->
            <template slot="title">
              <i :class="iconObj[item.id]" class="iconfont"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              v-for="ite in item.children"
              :key="ite.id"
              :index="'/'+ite.path"
              @click="saveNavState('/'+ite.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ ite.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧主体 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menulist: [],
      iconObj: {
        125: "iconfont icon-users",
        103: "iconfont icon-shikong-kongjianxuanzhong",
        101: "iconfont icon-baobao",
        102: "iconfont icon-danju",
        145: "iconfont icon-baobiao",
      },
      //是否折叠
      isCollapse: false,
      //
      activePath:'',
    }
  },
  created() {
    this.getMenuList();
    this.activePath=window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout() {
      window.sessionStorage.clear() //清空sessionStorage
      this.$router.push("/login") //回到登录界面
    },
    async getMenuList() {
      const { data: res } = await this.$http.get("menus") //get请求,并且解构赋值给res
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg) //请求失败的返回值
      this.menulist = res.data // 得到的数据res中的data赋值给menulist
      // console.log(this.menulist)
    },
    toggleColl() {
      this.isCollapse = !this.isCollapse
    },
    saveNavState(activePath){
      window.sessionStorage.setItem( "activePath",activePath) //保存path
      this.activePath=activePath   //重新赋值
    }
  },
}
</script>

<style lang="less" scoped>
.el-container {
  background-color: #fff;
  width: 100%;
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 10px;
  > div {
    display: flex;
    align-items: center;
    img {
      width: 50px;
      margin-right: 20px;

      background-color: #fff;
      border-radius: 50%;
    }
    span {
      font-size: 20px;
      color: #fff;
    }
  }
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
.iconfont {
  margin-right: 10px;
}
.toggle-button {
  background-color: #4a5064;
  text-align: center;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  letter-spacing: 0.2em;
}
</style>